package cn.myfirefly.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.myfirefly.utils.mail.MailInfo;
import cn.myfirefly.utils.mail.MailSender;

public class MailUtil {
	private static Logger logger = LoggerFactory.getLogger(MailUtil.class);
	
	private static ThreadPoolExecutor executorPool = new ThreadPoolExecutor(5, 20, 0L, TimeUnit.MILLISECONDS, 
			new ArrayBlockingQueue<Runnable>(5),
			new BasicThreadFactory.Builder().namingPattern("mail-send-%d").daemon(true).build());
	
	public static void sendMail(String to, String subject, String content) {
		final List<MailInfo> emailList = new ArrayList<>();
		MailInfo mailInfo = new MailInfo();
		mailInfo.setNotifyTo(to);
		mailInfo.setSubject(subject);
		mailInfo.setContent(content);
		mailInfo.setMessageType(MailInfo.MESSAGE_TYPE_HTML);
		emailList.add(mailInfo);
		executorPool.execute(new Runnable() {
			@Override
			public void run() {
				try {
					MailSender.sendMail(MailSender.getMailConfigInfo(), emailList, 3, null);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
				}
			}
		});
	}
}
